<template>
  <scroll-view class="scroll-list" scroll-y="true">
    <view class="container">
      <uni-list>
        <uni-list-item
          v-for="item in dataList"
          :key="item.name"
          :title="item.name"
          :show-arrow="true"
          :clickable="true"
          @click="handleShow(item)" />
      </uni-list>
    </view>
  </scroll-view>
</template>

<script>
  import { SqliteDataBase } from '@daelui/dogjs/dist/components.js'
  import sqliteSer from '@/api/service/sqlite.js'
  export default {
    data() {
      return {
        // 当前的数据列表
        dataList: []
      }
    },

    onShow() {
			this.initData()
    },
		onNavigationBarButtonTap (item) {
      if (item.category === 'edit') {
        this.handleEdit()
      } else if (item.category === 'delete') {
        this.handleDelete()
      }
		},
		onPullDownRefresh() {
			this.initData()
			setTimeout(function () {
				uni.stopPullDownRefresh()
			}, 1000)
		},
    methods: {
			initData () {
        let params = this.getParams()
        if (params.name) {
          uni.setNavigationBarTitle({
            title: params.name
          })
        }
				this.handleSearch()
			},
      // 搜索数据
      handleSearch () {
        this.dataList = []
        // 查询养护方法
        this.loadDataList()
      },
      // 初始化病害列表
      loadDataList () {
        let params = this.getParams()
        // 查询列表
        sqliteSer.queryList(params).then(list => {
          let node = list[0]
          if (node) {
            let db = new SqliteDataBase({
              dbName: String(node.fileName).replace(/.db$/i, ''),
              dbPath: node.filePath
            })
            db.queryTables().then(list => {
              this.dataList = list
            })
            this.db = db
          }
        })
      },
			handleShow (item) {
        uni.setStorageSync('db_table', {
          dbName: this.db.dbName,
          dbPath: this.db.dbPath,
          table: item.name
        })
        this.db.closeDB()
				this.$tab.navigateTo('/pages/tools/sqlite/view')
			},
      handleEdit () {
        let params = this.getParams()
				this.$tab.navigateTo('/pages/tools/sqlite/edit?id=' + params.id) 
      },
      handleDelete () {
        let params = this.getParams()
        // 删除
        uni.showModal({
          title: '提示',
          content: '确认删除吗?',
          success: (res) => {
            if (res.confirm) {
              sqliteSer.delete({ id: params.id }).then(() => {
                this.goBack()
              }).catch(() => {
                this.$u.toast('删除失败')
              })
            }
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  page {
    background-color: #ffffff;
		.d-text-btns {
			padding-top: 24px
		}
  }
</style>
